﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.BIM;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class RiserDuctCreateForm : System.Windows.Forms.Form
	{
		public RiserDuctCreateForm(Autodesk.Revit.DB.Document doc, List<string> levelUp, List<string> ductSystemType, Duct ductOld)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.CenterToParent();
			this.doc = doc;
			this.levelUp = levelUp;
			this.ductOld = ductOld;
			this.ductSystemType = ductSystemType;
			base.Icon = Resource.YArchitechIcon;
		}

		private void initDuctSize()
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(this.ductOld);
			if (hypiping.StartConnector.Connector.Shape == ConnectorSharp.Rect)
			{
				this.ductSize = new DuctSize(0.0, 0.0);
			}
			else if (hypiping.StartConnector.Connector.Shape == ConnectorSharp.Round)
			{
				this.ductSize = new DuctSize(0.0);
			}
			else if (hypiping.StartConnector.Connector.Shape == ConnectorSharp.Oval)
			{
				this.ductSize = new DuctSize(2, 0.0, 0.0);
			}
			else
			{
				this.ductSize = new DuctSize(0.0);
			}
			if (this.ductSize.Section == 1)
			{
				this.ChangeRoundSizeVisible(false);
				return;
			}
			if (this.ductSize.Section == 0)
			{
				this.ChangeRoundSizeVisible(true);
				return;
			}
			if (this.ductSize.Section == 2)
			{
				this.ChangeRoundSizeVisible(false);
			}
		}

		private void GetUserData()
		{
			string[] riserDuctText = this.userSetting.GetRiserDuctText();
			if (this.ductSize.Section == 1 || this.ductSize.Section == 2)
			{
				this.hytbHeight.Text = Convert.ToString(this.ductOld.Height * 304.8);
				this.hytbWidth.Text = Convert.ToString(this.ductOld.Width * 304.8);
			}
			else if (this.ductSize.Section == 0)
			{
				this.textBoxPipeDiameter.Text = Convert.ToString(this.ductOld.Diameter * 304.8);
			}
			for (int i = 3; i < 10; i++)
			{
				if (!string.IsNullOrEmpty(riserDuctText[i]))
				{
					switch (i)
					{
					case 3:
						this.comboBoxPipingSystemType.Text = riserDuctText[i];
						break;
					case 4:
						this.checkStartLevel.Checked = Convert.ToBoolean(riserDuctText[i]);
						break;
					case 5:
						this.comboxStartLevels.Text = riserDuctText[i];
						this.strLevelStart = riserDuctText[i];
						break;
					case 6:
						this.checkEndLevel.Checked = Convert.ToBoolean(riserDuctText[i]);
						break;
					case 7:
						this.comboxEndLevels.Text = riserDuctText[i];
						this.strLevelEnd = riserDuctText[i];
						break;
					case 8:
						this.textBoxOffsetDown.Text = riserDuctText[i];
						break;
					case 9:
						this.textBoxOffsetUp.Text = riserDuctText[i];
						break;
					}
				}
			}
			if (string.IsNullOrEmpty(this.comboBoxPipingSystemType.Text) && this.comboBoxPipingSystemType.Items != null && this.comboBoxPipingSystemType.Items.Count > 0)
			{
				this.comboBoxPipingSystemType.SelectedItem = this.comboBoxPipingSystemType.Items[0];
			}
		}

		private void SetUserData()
		{
			string[] texts = new string[]
			{
				this.hytbHeight.Text,
				this.hytbWidth.Text,
				this.textBoxPipeDiameter.Text,
				this.comboBoxPipingSystemType.SelectedItem.ToString(),
				this.checkStartLevel.Checked.ToString(),
				this.comboxStartLevels.SelectedItem.ToString(),
				this.checkEndLevel.Checked.ToString(),
				this.comboxEndLevels.SelectedItem.ToString(),
				this.textBoxOffsetDown.Text,
				this.textBoxOffsetUp.Text
			};
			this.userSetting.WriteRiserDuctText(texts);
		}

		private void buttonDraw_Click(object sender, EventArgs e)
		{
			if (this.textBoxPipeDiameter.Visible)
			{
				if (!Regex.IsMatch(this.textBoxPipeDiameter.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					MessageBox.Show("直径必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					this.textBoxPipeDiameter.Focus();
					return;
				}
				if (this.textBoxPipeDiameter.Text.Trim() == "")
				{
					MessageBox.Show("尺寸不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return;
				}
			}
			else if (this.hytbHeight.Visible)
			{
				if (!Regex.IsMatch(this.hytbHeight.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					MessageBox.Show("高度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					this.hytbHeight.Focus();
					return;
				}
				if (!Regex.IsMatch(this.hytbWidth.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					MessageBox.Show("宽度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					this.hytbWidth.Focus();
					return;
				}
				if (this.hytbHeight.Text.Trim() == "" || this.hytbWidth.Text.Trim() == "")
				{
					MessageBox.Show("尺寸不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return;
				}
			}
			if (!Regex.IsMatch(this.textBoxOffsetDown.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffsetDown.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxOffsetUp.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffsetUp.Focus();
				return;
			}
			if (this.textBoxOffsetDown.Text != "" && this.textBoxOffsetUp.Text != "")
			{
				if (this.checkStartLevel.Checked)
				{
					this.zStart = this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboxStartLevels.Text);
				}
				else
				{
					this.zStart = this.zCoord(double.Parse(this.textBoxOffsetDown.Text));
				}
				if (this.checkEndLevel.Checked)
				{
					this.zEnd = this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboxEndLevels.Text);
				}
				else
				{
					this.zEnd = this.zCoord(double.Parse(this.textBoxOffsetUp.Text));
				}
				MechanicalSystemType mechanicalSystemType = this.Get_DuctSystemType(this.doc, this.comboBoxPipingSystemType.Text);
				this.ductSystemTypeRiser = mechanicalSystemType;
				if (this.ductSize.Section == 0)
				{
					this.ductSize.Diameter = double.Parse(this.textBoxPipeDiameter.Text);
				}
				else if (this.ductSize.Section == 1)
				{
					this.ductSize.Height = double.Parse(this.hytbHeight.Text);
					this.ductSize.Width = double.Parse(this.hytbWidth.Text);
				}
				else if (this.ductSize.Section == 2)
				{
					this.ductSize.Height = double.Parse(this.hytbHeight.Text);
					this.ductSize.Width = double.Parse(this.hytbWidth.Text);
				}
				this.SetUserData();
				base.DialogResult = DialogResult.OK;
				return;
			}
			MessageBox.Show("偏移量不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void buttonEsc_Click_1(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			foreach (string item in this.levelUp)
			{
				this.comboxStartLevels.Items.Add(item);
				this.comboxEndLevels.Items.Add(item);
			}
			this.comboxStartLevels.SelectedItem = this.comboxStartLevels.Items[0];
			this.comboxEndLevels.SelectedItem = this.comboxEndLevels.Items[0];
			foreach (string item2 in this.ductSystemType)
			{
				this.comboBoxPipingSystemType.Items.Add(item2);
			}
			this.initDuctSize();
			this.strLevelStart = this.comboxStartLevels.Text;
			this.strLevelEnd = this.comboxEndLevels.Text;
			this.checkEndLevel.Checked = false;
			this.checkStartLevel.Checked = false;
			this.GetUserData();
			this.isInint = true;
			this.comStartLevelEnable(this.checkStartLevel.Checked);
			this.comEndLevelEnable(this.checkEndLevel.Checked);
		}

		private double zCoord(double offset)
		{
			return 0.0 + offset / 0.3048;
		}

		private DuctType Get_DuctType(Autodesk.Revit.DB.Document document, string ductTypeName)
		{
			return YJKRevitTools.GetSameNameElement(YJKElementFilter.GetElementByClass(typeof(DuctType), document), ductTypeName) as DuctType;
		}

		private MechanicalSystemType Get_DuctSystemType(Autodesk.Revit.DB.Document document, string ductSystemTypeName)
		{
			return YJKRevitTools.GetSameNameElement(YJKElementFilter.GetElementByClass(typeof(MechanicalSystemType), document), ductSystemTypeName) as MechanicalSystemType;
		}

		private void ChangeRoundSizeVisible(bool visible)
		{
			this.lbDiameter.Visible = visible;
			this.textBoxPipeDiameter.Visible = visible;
			this.lbHeight.Visible = !visible;
			this.lbWidth.Visible = !visible;
			this.hytbHeight.Visible = !visible;
			this.hytbWidth.Visible = !visible;
		}

		private void checkStartLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isInint)
			{
				this.comStartLevelEnable(this.checkStartLevel.Checked);
			}
		}

		private void checkEndLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isInint)
			{
				this.comEndLevelEnable(this.checkEndLevel.Checked);
			}
		}

		private double getLevelOffset(string level)
		{
			double result = 0.0;
			if (string.IsNullOrEmpty(level))
			{
				return result;
			}
			return this.Get_Level(this.doc, level).ProjectElevation;
		}

		private Level Get_Level(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as Level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void comboxStartLevels_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkStartLevel.Checked && this.isInint)
			{
				this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 + this.getLevelOffset(this.strLevelStart) * 0.3048 - this.getLevelOffset(this.comboxStartLevels.Text) * 0.3048, 3).ToString();
				this.strLevelStart = this.comboxStartLevels.Text;
			}
		}

		private void comboxEndLevels_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkEndLevel.Checked && this.isInint)
			{
				this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 + this.getLevelOffset(this.strLevelEnd) * 0.3048 - this.getLevelOffset(this.comboxEndLevels.Text) * 0.3048, 3).ToString();
				this.strLevelEnd = this.comboxEndLevels.Text;
			}
		}

		private void comStartLevelEnable(bool bEnable = false)
		{
			if (this.isInint)
			{
				if (bEnable)
				{
					this.label_Start.Text = "相对标高(m)";
					this.comboxStartLevels.Enabled = true;
					this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 - this.getLevelOffset(this.comboxStartLevels.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label_Start.Text = "绝对标高(m)";
				this.comboxStartLevels.Enabled = false;
				this.textBoxOffsetDown.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboxStartLevels.Text)) * 0.3048, 3).ToString();
			}
		}

		private void comEndLevelEnable(bool bEnable = false)
		{
			if (this.isInint)
			{
				if (bEnable)
				{
					this.label_End.Text = "相对标高(m)";
					this.comboxEndLevels.Enabled = true;
					this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 - this.getLevelOffset(this.comboxEndLevels.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label_End.Text = "绝对标高(m)";
				this.comboxEndLevels.Enabled = false;
				this.textBoxOffsetUp.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboxEndLevels.Text)) * 0.3048, 3).ToString();
			}
		}

		private Document doc;

		private List<string> levelUp = new List<string>();

		private List<string> ductSystemType = new List<string>();

		private Duct ductOld;

		public double zStart;

		public double zEnd;

		private string strLevelStart = "";

		private string strLevelEnd = "";

		public MechanicalSystemType ductSystemTypeRiser;

		public DuctSize ductSize;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private bool isInint;
	}
}
